﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace Password_manager
{
    public class UserPrefs
    {
        RegistryKey key;
        String keyname = "SOFTWARE\\Intel\\Tutorial Password Manager with Intel SGX";
        int _lockDelay;
        public int lockDelay {
            get
            {
                if (_lockDelay < 0) return 0;
                if (_lockDelay > 10) return 10;
                else return _lockDelay;
            }
            set {
                if (value < 0) _lockDelay = 0;
                else if (value > 10) _lockDelay = 10;
                else _lockDelay = value;
            }
        }
        int def_lockDelay = 0;

        public UserPrefs()
        {
            key = Registry.CurrentUser.OpenSubKey(keyname, true);
            if (key == null)
            {
                this.lockDelay = def_lockDelay;
            }
            else
            {
                this.lockDelay = (int)key.GetValue("LockDelay", def_lockDelay);
            }
        }

        public void SavePrefs ()
        {
            if ( key == null )
            {
                key = Registry.CurrentUser.CreateSubKey(keyname);
                if (key == null) return;
            }

            key.SetValue("LockDelay", lockDelay, RegistryValueKind.DWord);
        }
    }
}
